//Activity to set the override temperature

package g1.thermostat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

public class T4Activity extends Activity {

	ImageButton ovUp;
	ImageButton ovUp2;
	ImageButton ovDown;
	ImageButton ovDown2;
	Button btnCancel4;
	Button btnSave4;
	TextView ovTemp;
	CheckBox ovSch;
	CheckBox enableOv;
	double vot;
	boolean ovSchedule;
	boolean ovEnable;


	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout4);
		
		vot = ((Variables) this.getApplication()).getOvTemperature();
		//ovSchedule = ((Variables) this.getApplication()).getOvSchedule();
		//ovEnable = ((Variables) this.getApplication()).getOvEnable();
		
		
		ovSch = (CheckBox) findViewById( R.id.ovSch );
		enableOv = (CheckBox) findViewById( R.id.enableOv);
		ovUp = (ImageButton) findViewById(R.id.ovUp);
		ovUp2 = (ImageButton) findViewById(R.id.ovUp2);
		ovDown = (ImageButton) findViewById(R.id.ovDown);
		ovDown2 = (ImageButton) findViewById(R.id.ovDown2);
		ovTemp = (TextView) findViewById(R.id.ovTemp);
		btnCancel4 = (Button) findViewById(R.id.btnCancel4);
		btnSave4 = (Button) findViewById(R.id.btnSave4);
		
		ovTemp.setText(Double.toString(vot));
		ovSch.setEnabled(ovEnable);

		ovUp.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (vot <= 29) {
					vot++;				
				} else if (vot > 29 && vot < 30) {
					vot = 30;
				}
				
				if(vot==30){
					ovUp.setEnabled(false);
					ovUp2.setEnabled(false);
				}

				ovTemp.setText(Double.toString(vot));
				ovDown.setEnabled(true);
				ovDown2.setEnabled(true);
				
//				((Variables)getApplication()).setOvTemperature(vot);
			}
		});

		ovUp2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (vot <= 29.9) {
					if (vot == 29.9){
						ovUp.setEnabled(false);
						ovUp2.setEnabled(false);
					}
					vot = vot + 0.1;
					vot = (double) Math.round(vot * 10) / 10;

				}
				ovDown.setEnabled(true);
				ovDown2.setEnabled(true);
				ovTemp.setText(Double.toString(vot));
//				((Variables)getApplication()).setOvTemperature(vot);
			}
		});

		ovDown.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (vot >= 6) {
					vot--;

				} else if (vot < 6 && vot > 5) {
					vot = 5;
				
				}
				
				if(vot==5){
					ovDown.setEnabled(false);
					ovDown2.setEnabled(false);
				}
				ovUp.setEnabled(true);
				ovUp2.setEnabled(true);
				ovTemp.setText(Double.toString(vot));
//				((Variables)getApplication()).setOvTemperature(vot);
			}
		});
		ovDown2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (vot >= 5.1) {
					if (vot == 5.1){
						ovDown.setEnabled(false);
						ovDown2.setEnabled(false);
					}
					vot = vot - 0.1;
					vot = (double) Math.round(vot * 10) / 10;
				}
				
				ovUp.setEnabled(true);
				ovUp2.setEnabled(true);
				ovTemp.setText(Double.toString(vot));
//				((Variables)getApplication()).setOvTemperature(vot);
			}
		});
		
		
		ovSch.setOnCheckedChangeListener(new OnCheckedChangeListener()
			{
			    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
			    {
			        if ( isChecked )
			        {
			        	((Variables)getApplication()).setOvSchedule(true);
			        }
			        else{
			        	((Variables)getApplication()).setOvSchedule(false);
			        }

			    }
			});
		
		ovSch.setOnCheckedChangeListener(new OnCheckedChangeListener()
			{
			    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
			    {
			        if ( isChecked )
			        {
			        	ovSchedule=true;		        	
			        }
			        else{
			        	ovSchedule=false;
			        }
			        
//			        ((Variables)getApplication()).setOvSchedule(ovSchedule);
			    }
			});

		enableOv.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{
		    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
		    {
		        if ( isChecked )
		        {
		        	ovEnable=true;		        	
		        }
		        else{
		        	ovEnable=false;
		        }
		        
//		        ((Variables)getApplication()).setOvEnable(ovEnable);
		        ovSch.setEnabled(ovEnable);
		    }
		});
		
		btnCancel4.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				((Variables)getApplication()).setSave4(false);
//				Intent intent = new Intent();
//				intent.setClass(T4Activity.this, T1Activity.class);
//				startActivity(intent);			
				finish();
			}	

		});
		
		btnSave4.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				((Variables)getApplication()).setSave4(true);
				((Variables)getApplication()).setOvTemperature(vot);
				((Variables)getApplication()).setOvSchedule(ovSchedule);
		        ((Variables)getApplication()).setOvEnable(ovEnable);
//				Intent intent = new Intent();
//				intent.setClass(T4Activity.this, T1Activity.class);
//				startActivity(intent);			
		        finish();
			}	

		});
		
	}
	public void onResume(){
		super.onResume();
		vot = ((Variables) this.getApplication()).getOvTemperature();
		ovTemp.setText(Double.toString(vot));
		ovSchedule = ((Variables) this.getApplication()).getOvSchedule();
		ovEnable = ((Variables) this.getApplication()).getOvEnable();		
		enableOv.setChecked(ovEnable);	
		ovSch.setChecked(ovSchedule);
		
	}
}
